package com.dd.cloud.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.cloud.user.entity.order.OrderPay;
import com.dd.cloud.user.req.drugs.*;
import com.dd.cloud.user.req.pres.PresPageReq;
import com.dd.cloud.user.res.drugs.*;
import com.dd.cloud.user.res.pres.DrugsModelManagePageRes;
import com.dd.cloud.user.service.drugs.IDrugsOutboundService;
import com.dd.cloud.user.service.drugs.IDrugsWarehousingService;
import com.dd.cloud.user.service.pres.IDrugsModelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 药品控制器
 *
 * @author zk
 */
@RestController
@RequestMapping("/drugs")
@Validated
@Slf4j
public class DrugsController {
    @Autowired
    private IDrugsModelService drugsModelService;
    @Autowired
    private IDrugsWarehousingService drugsWarehousingService;
    @Autowired
    private IDrugsOutboundService drugsOutboundService;


    /**
     * 分页查询药品
     *
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<Page<DrugsModelManagePageRes>> getDrugsModelPage(@Valid PresPageReq req) {
        Page<DrugsModelManagePageRes> res = drugsModelService.getDrugsModelManagePage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 平台药房导入中药材详细信息数据
     *
     * @param file excel文件
     * @return 导入经验方数据
     */
    @PostMapping("/phar/import/excel")
    public ResponseEntity<Map<String, String>> importExcelDrugs(MultipartFile file) {
        Map<String, String> stringIntegerMap = drugsModelService.importExcelPharDrugs(file);
        return ResponseEntity.ok(stringIntegerMap);
    }

    /**
     * 药品入库
     *
     * @param req
     * @return 导入经验方数据
     */
    @PostMapping("/ware/add")
    public ResponseEntity<Void> wareAdd(@Valid @RequestBody AddDrugsWarehousingReq req) {
        drugsWarehousingService.addDrugsWarehousing(req);
        return ResponseEntity.ok().build();
    }

    /**
     * 平台药房导入药品入库数据
     *
     * @param file excel文件
     * @return 导入经验方数据
     */
    @PostMapping("/ware/add/excel")
    public ResponseEntity<Map<String, String>> importExcelWare(MultipartFile file) {
        Map<String, String> stringIntegerMap = drugsWarehousingService.importExcelDrugsWare(file);
        return ResponseEntity.ok(stringIntegerMap);
    }

    /**
     * 分页查询药品入库记录
     *
     * @return
     */
    @GetMapping("/ware/page")
    public ResponseEntity<Page<DrugsWarePageRes>> getDrugsWarePage(@Valid DrugsWarePageReq req) {
        Page<DrugsWarePageRes> res = drugsWarehousingService.getDrugsWarePage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 分页查询药品出库记录 按照药品查询
     *
     * @return
     */
    @GetMapping("/outbound/page/group/drug")
    public ResponseEntity<Page<DrugsOutboundGroupPageRes>> getDrugsWarePage(@Valid DrugsOutboundGroupPageReq req) {
        Page<DrugsOutboundGroupPageRes> res = drugsOutboundService.getDrugsOutboundPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 导出excel-分页查询药品出库记录
     *
     * @return
     */
    @GetMapping("/outbound/drug/excel/export")
    public ResponseEntity<Page<DrugsOutboundGroupPageRes>> exportDrugsWareExcel(@Valid DrugsOutboundGroupPageReq req, HttpServletResponse response) {
        drugsOutboundService.exportDrugsOutboundExcel(req, response);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页查询药品出库记录 按照订单查询
     *
     * @return
     */
    @GetMapping("/outbound/page/group/order")
    public ResponseEntity<SummaryPage<DrugsOutboundOrderPageRes>> getDrugsWarePage(@Valid DrugsOutboundOrderPageReq req) {
        SummaryPage<DrugsOutboundOrderPageRes> res = drugsOutboundService.getDrugsOutboundOrderPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 导出excel-分页查询药品出库记录 按照订单查询
     *
     * @return
     */
    @GetMapping("/outbound/order/excel/export")
    public ResponseEntity<Page<DrugsOutboundGroupPageRes>> exportOrderWareExcel(@Valid DrugsOutboundOrderPageReq req, HttpServletResponse response) {
        drugsOutboundService.exportOrderOutboundExcel(req, response);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页查询药品库存记录
     *
     * @return
     */
    @GetMapping("/qty/page")
    public ResponseEntity<Page<DrugsQtyPageRes>> getDrugsQtyPage(@Valid DrugsQtyPageReq req) {
        Page<DrugsQtyPageRes> res = drugsWarehousingService.getDrugsQtyPage(req);
        return ResponseEntity.ok(res);
    }

    /**
     * 分页查询药品库存记录
     *
     * @return
     */
    @GetMapping("/isHave/qty")
    public ResponseEntity<List<DrugsQtyRes>> getDrugsIsHaveQty(@RequestParam("drugnums") String drugnums,
                                                               @RequestParam("pharId") Integer pharId) {
        List<DrugsQtyRes> res = drugsWarehousingService.getDrugsIsHaveQty(drugnums,pharId);
        return ResponseEntity.ok(res);
    }

    /**
     * 药材数据更新到es
     *
     * @return 导入经验方数据
     */
    @PostMapping("/list/es/update")
    public ResponseEntity<Void> drugsEsUpdate() {
        drugsModelService.drugsEsUpdate();
        return ResponseEntity.ok().build();
    }
}
